package com.ctgu.edu.controller;

import com.ctgu.edu.pojo.Account;
import com.ctgu.edu.pojo.Recruiter;
import com.ctgu.edu.pojo.Resume;
import com.ctgu.edu.pojo.Seeker;
import com.ctgu.edu.service.impl.AccountServiceImpl;
import com.ctgu.edu.service.impl.RecruiterServiceImpl;
import com.ctgu.edu.service.impl.SeekerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
public class RegisterAndLoginController {

    @Autowired
    @Qualifier("SeekerServiceImpl")
    private SeekerServiceImpl seekerServiceImpl;
    @Autowired
    @Qualifier("accountServiceImpl")
    private AccountServiceImpl accountServiceImpl;
    @Autowired
    @Qualifier("recruiterServiceImpl")
    private RecruiterServiceImpl recruiterServiceImpl;


    //首页跳转
    @RequestMapping("/welcome/{apply}")
    public String Welcome(@PathVariable String apply){
        return apply;
    }

    //求职者注册
    @RequestMapping("/seeker/regist")
    public String SeekerRegist(Account account,Seeker seeker, String reAccountKey,Model model){
        //两次输入密码不一致
        if(!reAccountKey.equals(account.getAccountKey())){
            model.addAttribute("inf","两次输入密码不一致！");
            return "regist";
        }
        //用户名已被占用
        if(accountServiceImpl.getAccountById(account.getAccountId())!=null){
            model.addAttribute("inf","用户名已被占用！");
            return "regist";
        }

        accountServiceImpl.addAccount(account);
        seeker.setSeekerAccountId(account.getAccountId());
        seekerServiceImpl.addSeeker(seeker);
        return "login";
    }

    //发布者注册
    @RequestMapping("/recruiter/regist")
    public String RecRegister(Account account, Recruiter recruiter, String reAccountKey,Model model){

        //两次输入密码不一致
        if(!reAccountKey.equals(account.getAccountKey())){
            model.addAttribute("inf","两次输入密码不一致！");
            return "regist_recruiter";
        }
        //用户名已被占用
        if(accountServiceImpl.getAccountById(account.getAccountId())!=null){
            model.addAttribute("inf","用户名已被占用！");
            return "regist_recruiter";
        }

        accountServiceImpl.addAccount(account);
        recruiter.setRecruiterAccountId(account.getAccountId());
        recruiterServiceImpl.addRecruiter(recruiter);
        return "login";
    }

    //处理登录
    @RequestMapping("/login")
    public String Login(Account account, Model model, HttpServletRequest request){
    // HttpServletRequest request:方便获取session

        Account standardAccount = accountServiceImpl.getAccountById(account.getAccountId());
        //检查账户
        if(standardAccount==null){
            model.addAttribute("inf_login","用户不存在！");

            //这样转发可以携带model，但是传统凡是为啥不可以？？？
            return "forward:welcome/login";
        }else{
            //判断密码不正确
            if(!account.getAccountKey().equals(standardAccount.getAccountKey())){
                model.addAttribute("inf_login","密码不正确！");
                return "login";
            }
        }

        //获取session
        HttpSession session = request.getSession();
        session.setAttribute("account",account);
        //判断用户类别为求职者
        Seeker seeker = seekerServiceImpl.getSeekerByAccountId(account.getAccountId());
        if(seeker != null){
            session.setAttribute("seeker",seeker);
            model.addAttribute("inf_login","成功登录为求职者！");
            return "forward:/seeker/mainpage/0/10";
        }
        //判断用户类别为招聘者
        Recruiter recruiter =recruiterServiceImpl.getRecruiterByAccountId(account.getAccountId());
        if(recruiter != null){
            session.setAttribute("recruiter",recruiter);
            model.addAttribute("inf_login","成功登录为招聘者！");

            return "forward:/recruiter/mainpage";
        }

        //牛马情况
        model.addAttribute("inf_login","内部错误！");
        return "login";
    }

    @RequestMapping("/account/edit/key")
    public String EditAccountKey(String key,String newKey,String newRekey,Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Account account =(Account) session.getAttribute("account"); //获取当前登录的用户
        if(!key.equals(account.getAccountKey())){
            model.addAttribute("inf_edit_key","原密码不正确");
            return "edit_account_key";
        }else if(!newKey.equals(newRekey)){
            model.addAttribute("inf_edit_key","两次输入密码不一致！！");
            return "edit_account_key";
        }else{
            account.setAccountKey(newKey);
            accountServiceImpl.updateAccount(account);
            session.setAttribute("account",account);
            return "forward:/welcome/login";
        }

    }

    @RequestMapping("/quit")
    public String Quit(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("account");
        session.removeAttribute("seeker");
        session.removeAttribute("recruiter");
        return "forward:/";
    }
}
